<?php

/**
 * Profile model
 */
class Profile{
	
	/**
	 * The registry
	 */
	public $registry;
	
	/**
	 * Profile ID
	 */
	private $id;
	
	/**
	 * Fields which can be saved by the save() method
	 */
	private $savable_profile_fields = array( 'name', 'photo', 'email', 'dob', 'gender', 'profile_name', 'about', 'address', 'employer', 'college', 'highschool');
	
	/**
	 * Users ID
	 */
	private $user_id;
	
	/**
	 * Users name
	 */
	private $name;
	
	/**
	 * Date of Birth
	 */
	private $dob;
	
	/**
	 * Gender
	 */
	private $gender;
	
	/**
	 * Users photograph
	 */
	private $photo;
	
	/** 
	 *  Flag to indeicate the default profile
	 */
	private $is_default;
	
	private $profile_name;
	
	private $about;
	
	private $email;
	
	private $address;
	
	private $employer;
	
	private $college;
	
	private $highschool;
	
	private $invalid_profile = true;
	
	private $access_token = '';
	
	private $profile_type = 0;
	
	public $friends = array();
	
	private $fofriends = array();
	
	/**
	 * Profile constructor
	 * @param Registry $registry the registry
	 * @param int $id the profile ID
	 * @return void
	 */
	public function __construct( Registry $registry, $user_id=0, $profile_id=0)
	{
		$this->registry = $registry;
		if( $user_id != 0 )
		{
			$this->id = $profile_id;
			$this->user_id = $user_id;
			// if an ID is passed, populate based off that
			$sql = 'SELECT * FROM profile WHERE user_id= '.$this->user_id.' AND profile_id='.$profile_id;
			$this->registry->getObject('db')->executeQuery( $sql );
			if( $this->registry->getObject('db')->numRows() == 1 )
			{	
				$data = $this->registry->getObject('db')->getRows();
				// populate our fields
				foreach( $data as $key => $value )
				{
					$this->$key = $value;
				}
				
				$this->invalid_profile = false;
			}
		}
		
		if ($this->profile_name == '')
			$this->profile_name = "Default";
		
		if ($this->photo == "")
		{
			$this->photo = "resources/images/default/profile.jpg";
			echo 'DEFAULT';
		}
		
		if ($this->profile_type == 2){
			//print_r($this->registry);
			
			//$this->fb_connector = $this->registry->getObject('fbconnector');
		}
	}
	
	/**
	 * Sets the users name
	 * @param String $name
	 * @return void
	 */
	public function setName( $name )
	{
		$this->name = $name;
	}
	
	/**
	 * Set the data of birth
	 * @param String $dob the date of birth
	 * @param boolean $formatted - indicates if the controller has formatted the dob, or if we need to do it here
	 */
	public function setDOB( $dob, $formatted=true )
	{
		echo $dob;
		if( $formatted == true )
		{
			$this->dob = $dob;
		}
		else
		{
			$temp = explode('/', $dob );
			$this->dob = $temp[2].'-'.$temp[1].'-'.$temp[0];
		}
	}
	
	public function getDOB()
	{
		return $this->dob;
	}
	
	
	/**
	 * Set the gender 
	 * @param String $gender the gender
	 * @param boolean $checked - indicates if the controller has validated the gender, or if we need to do it
	 * @return void
	 */
	public function setGender( $gender, $checked=true )
	{
		if( $checked == true )
		{
			$this->gender = $gender;
		}
		else
		{
			$genders = array();
			if( in_array( $gender, $genders ) )
			{
				$this->gender = $gender;
			}
		}
		
		echo $this->gender;
	}
	
	/**
	 * Sets the users about
	 * @param String about
	 * @return void
	 */
	public function setAbout( $about )
	{
		$this->about = $about;
	}
	
	/**
	 * Sets the users profile picture
	 * @param String photo name
	 * @return void
	 */
	public function setPhoto( $photo )
	{
		$this->photo = $photo;
	}
	
	public function setEmail($email)
	{
		$this->email = $email;
	}
	
	public  function setProfilename($profileName)
	{
		$this->profile_name = $profileName;
	}
	
	/**
	 * Save the user profile
	 * @return bool
	 */
	public function save()
	{
		// handle the updating of a profile
		if( $this->registry->getObject('authenticate')->isLoggedIn() &&
		 ( $this->registry->getObject('authenticate')->getUser()->getUserID() ==  $this->user_id || 
		 	$this->registry->getObject('authenticate')->getUser()->isAdmin() == true  ) )
		{
			// we are either the user whose profile this is, or we are the administrator
			$changes = array();
			foreach( $this->savable_profile_fields as $field )
			{
				$changes[ $field ] = $this->$field;
			}
			$success = $this->registry->getObject('db')->updateRecords( 'profile', $changes, ('user_id=' . $this->user_id.' AND profile_id = '.$this->id) );
			if( $success )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Convert the users profile data to template tags
	 * @param String $prefix prefix for the template tags
	 * @return void
	 */
	public function toTags( $prefix='' )
	{
		foreach( $this as $field => $data )
		{
			if( ! is_object( $data ) && ! is_array( $data ) )
			{
				$this->registry->getObject('template')->getPage()->addTag( $prefix.$field, $data );
			}
		}
		
		$genders = array();
		$genders[] = 'Male';
		$genders[] = 'Female';
		
		$gendervalues = array();
		
		foreach( $genders as $gender )
		{
			$values = array();
			$values['gender'] = $gender;
			
			if ($gender == $this->gender)
			{				
				$values['optionselected'] = "selected = \"selected\"";
			}
			else
				$values['optionselected'] = "";
			
			$gendervalues[] = $values;
		}
		
		$cache = $this->registry->getObject('db')->cacheData($gendervalues);		
		$this->registry->getObject('template')->getPage()->addTag( 'genderoptions', array( 'DATA', $cache) );		
	}
	
	/**
	 * Get the users name
	 * @return String
	 */
	public function getName()
	{
		return $this->name;
	}
	
	public function getProfileName()
	{
		return $this->profile_name;
	}
	
	public  function getIsDefault()
	{
		return $this->is_default;
	}
	
	/**
	 * Get the users photograph
	 * @return String
	 */
	public function getPhoto()
	{
		return $this->photo;
	}
	
	/**
	 * Get the users ID
	 * @return int
	 */
	public function getUserID()
	{
		return $this->user_id;
	}
	
	public function getProfileID()
	{
		return $this->id;
	}
	
	public function getAbout()
	{
		return $this->about;
	}
	
	public function getAddress()
	{
		return $this->address;
	}
	
	public function setAddress($address)
	{
		$this->address = $address;
	}
	
	public function getEmployer()
	{
		return $this->employer;
	}
	
	public function getCollege()
	{
		return $this->college;
	}
	
	public function getHighSchool()
	{
		return $this->highschool;
	}
	
	public function setEmployer($employer)
	{
		$this->employer = $employer;
	}
	
	public function setCollege($college)
	{
		$this->college = $college;
	}
	
	public function setHighSchool($highSchool)
	{
		$this->highschool = $highSchool;
	}
	
	public function getIsInvalidProfile(){
		return $this->invalid_profile;
	}
	
	public function getAccessToken(){
		return $this->access_token;
	}
	
	public function getProfileType(){
		return $this->profile_type;
	}
	
	public function setFriends($friend){
		$this->friends = $friend;
	}

	public function getFBFriendInfo($friendid){		
		$friendinfo = null;
		
		$fb_Connector = $this->registry->getObject('fbconnector');
		
		if (false == isset($this->friends[$friendid])){
			if (false == isset($this->fofriends[$friendid])){
				$info = $fb_Connector->getProfileInfo($friendid);
				$this->fofriends[$friendid] = $info[0];
				$this->fofriends[$friendid]['uid'] = $this->fofriends[$friendid]['id'];
				$this->fofriends[$friendid]['profile_url'] = $this->fofriends[$friendid]['url'];
				$this->fofriends[$friendid]['pic_small_with_logo'] = $this->fofriends[$friendid]['pic_small'];
			}
			$friendinfo = $this->fofriends[$friendid];
		}
		else{
			$friendinfo = $this->friends[$friendid];
		}
		
		return $friendinfo;
	}

	public function getInitFBConnector($loadfriend = true){
		$this->fb_connector = $this->registry->getObject('fbconnector');

		$app_id = $this->registry->getSetting('appid');
		$app_secret = $this->registry->getSetting('appsecret');
		$redirectURL = $this->registry->getObject('url')->buildURL( array('profile', 'home', $this->user_id, $this->id), 
			'', false );

		if ($this->fb_connector->getIsConnected() == false)
		{
			$this->fb_connector->ConnectwithToken($this->access_token, true);
			if ( ($this->friends == null) && ($loadfriend == true)){
				$friendlist = $this->fb_connector->getFriends($this->fb_connector->getUserID());

				foreach ($friendlist as $friend){
					$this->friends[$friend['uid']] = $friend;
				}
			}
		}

		return $this->fb_connector;
	}
}


?>